package com.ctl.utils.redis;

import java.util.Arrays;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;

import java.util.ArrayList;
import java.util.List;

public class RedisShardedJedisPipelinePool {
	// 分布式连接池异步调用
	public static void main(String[] args) {

        // 配置信息
        String host = "localhost"; // 替换为 RedisConfig.host
        int port = 6379;           // 替换为 RedisConfig.port
        String auth = "your_password"; // 替换为 RedisConfig.auth

        // 创建Jedis池配置
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // 设置最大连接数等其他配置...

        try (JedisPool jedisPool = new JedisPool(poolConfig, host, port)) {
            try (Jedis jedis = jedisPool.getResource()) {
                if (!auth.isEmpty()) {
                    jedis.auth(auth);
                }

                long start = System.currentTimeMillis();
                try (Pipeline jedisPipeline = jedis.pipelined()) {
                    for (int i = 0; i < 100; i++) {
                        jedisPipeline.set("sppn" + i, "n" + i);
                    }
                    List<Object> results = jedisPipeline.syncAndReturnAll();
                    long end = System.currentTimeMillis();
                    System.out.println("Pipelined SET: " + ((end - start) / 1000.0) + " seconds results.size()=" + results.size());
                }
            }
        }
	}
}
